﻿
//==============================================================================
// 文件     ：JSONHelper.cs
// 功能概要 ：序列化类以网络传输
//           
//           
// 作者    ：周薇
// 时间    ：2011年5月16日
// Copyright (c) 2011 重庆安运科技公司. All rights reserved.
//==============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;

namespace Webcar.Utils
{
    /// <summary>
    /// 序列化类以网络传输
    /// </summary>
    public class JsonHelper
    {
        static JsonHelper()
        {
            compressor = new Compressor(10);
        }
        private static Compressor compressor;
        /// <summary>
        /// 得到类的json字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetJson(object obj)
        {
            DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                json.WriteObject(stream, obj);
                return Encoding.UTF8.GetString(stream.ToArray());
            }
        }
        /// <summary>
        /// 从json生成类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="szJson"></param>
        /// <returns></returns>
        public static T ParseFromJson<T>(string szJson)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(szJson);
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(ms);
            }
        }
        /// <summary>
        /// 类转换为json然后压缩
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] GetJsonZip(object obj)
        {
            DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());

            using (MemoryStream stream = new MemoryStream())
            {
                json.WriteObject(stream, obj);
                return compressor.Compress(stream.ToArray());
            }
        }
        /// <summary>
        /// 从压缩的json数据流中生成类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T PraseFromJsonZip<T>(byte[] json)
        {
            //T obj = Activator.CreateInstance<T>();
            byte[] bytes = compressor.Decompress(json);

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(ms);
            }
        }
    }
}
